/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* detectCycle(struct ListNode* head)
{
    ListNode* fast = head;
    ListNode* slow = head;
    if (fast == NULL)
        return NULL;

    while (fast != NULL && fast->next != NULL && fast->next->next != NULL)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (fast == slow)
            break;
    }
    if (fast == slow && fast->next != NULL && fast->next->next != NULL)
    {
        ListNode* phead = head;
        ListNode* meet = fast;
        while (meet != phead)
        {
            phead = phead->next;
            meet = meet->next;
        }
        return meet;
    }
    return NULL;


}